package com.carey.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carey.myblog.exception.NotFoundException;
import com.carey.myblog.pojo.Blog;
import com.carey.myblog.pojo.Type;
import com.carey.myblog.repository.TypeRepository;
import com.carey.myblog.service.BlogService;
import com.carey.myblog.service.BlogTagsService;
import com.carey.myblog.service.TypeService;
import com.carey.myblog.mapper.TypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* @author carey-
* @description 针对表【t_type】的数据库操作Service实现
* @createDate 2022-05-25 22:10:00
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
    implements TypeService{

    @Autowired
    private TypeRepository typeRepository;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagsService blogTagsService;

    @Transactional
    @Override
    public int saveType(Type type) {
        return baseMapper.insert(type);
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        return baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public Type getTypeByName(String name) {
        QueryWrapper<Type> wrapper = new QueryWrapper();
        wrapper.eq("name",name);
        return baseMapper.selectOne(wrapper);
    }

    @Transactional
    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }

    @Transactional
    @Override
    public Type updateType(Long id, Type type) {
        Type type1 = this.getType(id);
        if (null==type1){
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(type,type1);
        baseMapper.updateById(type1);
        return type1;
    }

    @Override
    public void deleteType(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<Type> listTypeTop(Integer size) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        List<Type> types = baseMapper.selectList(wrapper);
        //截取前size个元素
        //判断索引是否会越界
        if (size>types.size()){
            return this.getBlogByTypes(types);
        }
        return this.getBlogByTypes(types).subList(0,size-1);
    }

    public List<Type> getBlogByTypes(List<Type> list){
        list.forEach(type ->{

            QueryWrapper<Blog> wrapper = new QueryWrapper<>();
            wrapper.eq("type_id",type.getId());
            wrapper.eq("published",true);
            List<Blog> blogList = blogService.list(wrapper);
            type.setBlogs(blogList);
        });
        return this.Descsort(list);
    }

    /**
     * 根据集合的size进行降序排序
     * @param types
     * @return
     */
    public List<Type> Descsort(List<Type> types) {
        types.sort(new Comparator<Type>() {
            @Override
            public int compare(Type o1, Type o2) {
                return o2.getBlogs().size()-o1.getBlogs().size();
            }
        });
        return types;
    }


}




